<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class worker extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'songshu:worker';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '实时获取数据统计';

    private static $min_id = 0;
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        while(true){
            $min_id = SELF::$min_id;
            $data = DB::table('partner_task')->where('status',0)->where('id','>',$min_id)->orderBy('id','asc')->first();
            if(!empty($data)){
                $function = 'worker'.$data->type;
                $res = $this->$function($data);
                if($res==false){
                    echo "error_id:".$data->id."\n";
                    SELF::$min_id = $data->id;
                }else{
                    echo $data->id."\n";
                }
            }
            DB::disconnect('mysql');
            usleep(500000);
        }
        
    }

    /**
     * 今日新增补贴
     */
    public function worker1($data){
        $day = date('Ymd',$data->time);
        $ym = date('Ym',$data->time);
        $line =DB::table('partner_daily_data')->where('uid',$data->tree_id)->where('day',$day)->first();
        DB::beginTransaction();
        if(empty($line)){
            $p = [
                'uid'=>$data->tree_id,
                'from_uid'=>$data->from_uid,
                'coin_all'=>$data->coin,
                'subsidy_coin'=>$data->coin,
                'day'=>$day,
                'ym'=>$ym,
            ];
            $step1 = DB::table('partner_daily_data')->insert($p);
        }else{
            $step1 = DB::table('partner_daily_data')->where('uid',$data->tree_id)->where('day',$day)->update([
                'coin_all'=>DB::raw("coin_all+".$data->coin),
                'subsidy_coin'=>DB::raw("subsidy_coin+".$data->coin),
            ]);
        }
        $step2 = DB::table('partner_task')->where('id',$data->id)->update([
            'status'=>1
        ]);
        if($step1 && $step2){
            DB::commit();
            return true;
        }else{
            DB::rollBack();
            return false;
        }
    }

    /**
     * 今日分成收益
     */
    public function worker2($data){
        $day = date('Ymd',$data->time);
        $ym = date('Ym',$data->time);
        $line =DB::table('partner_daily_data')->where('uid',$data->tree_id)->where('day',$day)->first();
        DB::beginTransaction();
        if(empty($line)){
            $p = [
                'uid'=>$data->tree_id,
                'from_uid'=>$data->from_uid,
                'coin_all'=>$data->coin,
                'divide_into_coin'=>$data->coin,
                'day'=>$day,
                'ym'=>$ym,
            ];
            $step1 = DB::table('partner_daily_data')->insert($p);
        }else{
            $step1 = DB::table('partner_daily_data')->where('uid',$data->tree_id)->where('day',$day)->update([
                'coin_all'=>DB::raw("coin_all+".$data->coin),
                'divide_into_coin'=>DB::raw("divide_into_coin+".$data->coin),
            ]);
        }
        $step3 = true;
        if($data->from_uid>0){
            $coin = 0.5*$data->coin;
            $line =DB::table('partner_daily_data')->where('uid',$data->from_uid)->where('day',$day)->first();
            if(empty($line)){
                $p = [
                    'uid'=>$data->from_uid,
                    'from_uid'=>0,
                    'coin_all'=>$coin,
                    'divide_into_coin'=>$coin,
                    'subgrade_coin'=>$coin,
                    'day'=>$day,
                    'ym'=>$ym,
                ];
                $step3 = DB::table('partner_daily_data')->insert($p);
            }else{
                $step3 = DB::table('partner_daily_data')->where('uid',$data->from_uid)->where('day',$day)->update([
                    'coin_all'=>DB::raw("coin_all+".$coin),
                    'divide_into_coin'=>DB::raw("divide_into_coin+".$coin),
                    'subgrade_coin'=>DB::raw("subgrade_coin+".$coin),
                ]);
            }
        }

        $step2 = DB::table('partner_task')->where('id',$data->id)->update([
            'status'=>1
        ]);
        if($step1 && $step2 && $step3){
            DB::commit();
            return true;
        }else{
            DB::rollBack();
            return false;
        }
    }

    /**
     * 今日活跃树
     */
    public function worker3($data){
        $day = date('Ymd',$data->time);
        $ym = date('Ym',$data->time);
        $line =DB::table('partner_daily_data')->where('uid',$data->tree_id)->where('day',$day)->first();
        DB::beginTransaction();
        if(empty($line)){
            $p = [
                'uid'=>$data->tree_id,
                'from_uid'=>$data->from_uid,
                'active_count'=>1,
                'day'=>$day,
                'ym'=>$ym,
            ];
            $step1 = DB::table('partner_daily_data')->insert($p);
        }else{
            $step1 = DB::table('partner_daily_data')->where('uid',$data->tree_id)->where('day',$day)->update([
                'active_count'=>DB::raw("active_count+1"),
            ]);
        }
        $step2 = DB::table('partner_task')->where('id',$data->id)->update([
            'status'=>1
        ]);
        if($step1 && $step2){
            DB::commit();
            return true;
        }else{
            DB::rollBack();
            return false;
        }
    }

    /**
     * 今日新增用户
     */
    public function worker4($data){
        $day = date('Ymd',$data->time);
        $ym = date('Ym',$data->time);
        $line =DB::table('partner_daily_data')->where('uid',$data->tree_id)->where('day',$day)->first();
        DB::beginTransaction();
        if(empty($line)){
            $p = [
                'uid'=>$data->tree_id,
                'from_uid'=>$data->from_uid,
                'added_count'=>1,
                'day'=>$day,
                'ym'=>$ym,
            ];
            $step1 = DB::table('partner_daily_data')->insert($p);
        }else{
            $step1 = DB::table('partner_daily_data')->where('uid',$data->tree_id)->where('day',$day)->update([
                'added_count'=>DB::raw("added_count+1"),
            ]);
        }
        $step2 = DB::table('partner_task')->where('id',$data->id)->update([
            'status'=>1
        ]);
        if($step1 && $step2){
            DB::commit();
            return true;
        }else{
            DB::rollBack();
            return false;
        }
    }

    /**
     * 今日徒弟 徒孙活跃数
     */
    public function worker5($data){
        $day = date('Ymd',$data->time);
        $ym = date('Ym',$data->time);
        $user = DB::table('user_tree')->where('uid',$data->uid)->first();
        $shifu = DB::table('user_tree')->where('uid',$user->pid)->first();

        
        if(!empty($shifu) && $shifu->user_level==2){
            $line =DB::table('partner_daily_data')->where('uid',$user->pid)->where('day',$day)->first();
            DB::beginTransaction();
            if(empty($line)){
               $p = [
                    'uid'=>$shifu->uid,
                    'from_uid'=>$shifu->from_uid,
                    'td_active'=>1,
                    'day'=>$day,
                    'ym'=>$ym,
                ];
                $step1 = DB::table('partner_daily_data')->insert($p);
                }else{
                    $step1 = DB::table('partner_daily_data')->where('uid',$shifu->uid)->where('day',$day)->update([
                    'td_active'=>DB::raw("td_active+1"),
                ]);
            }

            $step2 = DB::table('partner_task')->where('id',$data->id)->update([
                'status'=>1
            ]);
            if($step1 && $step2){
                DB::commit();
                return true;
            }else{
                DB::rollBack();
            }
            return true;
        }
       
        $shizu = DB::table('user_tree')->where('uid',$shifu->pid)->where('user_level',2)->first();
        if(!empty($shizu)){
           $line =DB::table('partner_daily_data')->where('uid',$shizu->uid)->where('day',$day)->first();
            DB::beginTransaction();
            if(empty($line)){
               $p = [
                    'uid'=>$shizu->uid,
                    'from_uid'=>$shizu->from_uid,
                    'ts_active'=>1,
                    'day'=>$day,
                    'ym'=>$ym,
                ];
                $step1 = DB::table('partner_daily_data')->insert($p);
                }else{
                    $step1 = DB::table('partner_daily_data')->where('uid',$shizu->uid)->where('day',$day)->update([
                    'ts_active'=>DB::raw("ts_active+1"),
                ]);
            }

            $step2 = DB::table('partner_task')->where('id',$data->id)->update([
                'status'=>1
            ]);
            if($step1 && $step2){
                DB::commit();
                return true;
            }else{
                DB::rollBack();
                return false;
            } 
        }else{
            DB::table('partner_task')->where('id',$data->id)->update(['status'=>1]);
            return true;
        }
    }


    /**
     * 正常阶段奖励
     */
    public function worker6($data){
        $day = date('Ymd',$data->time);
        $ym = date('Ym',$data->time);
        $line =DB::table('partner_daily_data')->where('uid',$data->uid)->where('day',$day)->first();
        DB::beginTransaction();
        if(empty($line)){
            $p = [
                'uid'=>$data->uid,
                'from_uid'=>$data->from_uid,
                'app_stage'=>$data->coin,
                'day'=>$day,
                'ym'=>$ym,
            ];
            $step1 = DB::table('partner_daily_data')->insert($p);
        }else{
            $step1 = DB::table('partner_daily_data')->where('uid',$data->uid)->where('day',$day)->update([
                'app_stage'=>DB::raw("app_stage+".$data->coin),
            ]);
        }
        $step2 = DB::table('partner_task')->where('id',$data->id)->update([
            'status'=>1
        ]);
        if($step1 && $step2){
            DB::commit();
            return true;
        }else{
            DB::rollBack();
            return false;
        }
    }

    /**
     * 分成奖励
     */
    public function worker7($data){
        $day = date('Ymd',$data->time);
        $ym = date('Ym',$data->time);
        $line =DB::table('partner_daily_data')->where('uid',$data->uid)->where('day',$day)->first();
        DB::beginTransaction();
        if(empty($line)){
            $p = [
                'uid'=>$data->uid,
                'from_uid'=>$data->from_uid,
                'app_tribute'=>$data->coin,
                'day'=>$day,
                'ym'=>$ym,
            ];
            $step1 = DB::table('partner_daily_data')->insert($p);
        }else{
            $step1 = DB::table('partner_daily_data')->where('uid',$data->uid)->where('day',$day)->update([
                'app_tribute'=>DB::raw("app_tribute+".$data->coin),
            ]);
        }
        $step2 = DB::table('partner_task')->where('id',$data->id)->update([
            'status'=>1
        ]);
        if($step1 && $step2){
            DB::commit();
            return true;
        }else{
            DB::rollBack();
            return false;
        }
    }
}
